package com.apache.common.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.UUIDUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname SynActToOrgManagerPluginImpl
 * @Description 新增机构数据权限同步到顶级机构管理员
 * @Date 2020/6/18 22:17
 * @Created by fpc
 */
public class SynActToOrgManagerPluginImpl implements CustomMethodPlugin {
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        String userOrgId = String.valueOf(map.get("userOrgId"));
        Map<String,String> idMap = ( Map<String,String>)map.get("resultIds");
        String orgId = idMap.get("uctOrg_orgId");
        Map whereMap = new HashMap();
        whereMap.put("uctUser.w_orgId",userOrgId);
        whereMap.put("uctUser.w_userType","manager");
        ResultEntity userResultEntity = IusPluginUtil.getInstance().select("plateform", "list", "s_uctUser", whereMap);
        if(!Validator.isEmpty(userResultEntity.getEntity())){
            List<DataMap> userList = (List<DataMap>)userResultEntity.getEntity();
            if(!Validator.isEmpty(userList) && userList.size()>0){
                StringBuilder sb = new StringBuilder();
                for (DataMap user:userList){
                    sb.append("insert into uct_data_right (data_id,user_id,data_name,data_key) values('");
                    sb.append(UUIDUtil.generateId()+"','");
                    sb.append(user.get("userId")+"','org','");
                    sb.append(orgId+"');");
                }
                System.out.println("同步更新顶级机构管理员数据机构权限sql---->>>"+sb.toString());

                boolean b = IusPluginUtil.getInstance().saveBatchInvoke("plateform", sb.toString());
                System.out.println("执行结果："+b);

            }
        }
        return null;
    }
}
